<template>
   <el-form ref="pwdRef" :model="user" :rules="rules" label-width="80px">
      <el-form-item label="旧密码" prop="oldPass">
         <el-input v-model="user.oldPass" placeholder="请输入旧密码" type="password" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
         <el-input v-model="user.pass" placeholder="请输入新密码" type="password" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
         <el-input v-model="user.confirmPassword" placeholder="请确认新密码" type="password" show-password/>
      </el-form-item>
      <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
      <el-button type="danger" @click="close">关闭</el-button>
      </el-form-item>
   </el-form>
</template>

<script setup>
import request from '@/utils/request'
import { getPassTimeOut, setPassTimeOut, removePassTimeOut } from '@/utils/auth'
import { Encrypt, Decrypt } from "@/utils/AES256";
import useUserStore from '@/store/modules/user'
import { useRoute, useRouter } from 'vue-router'

const router = useRouter();
const userStore = useUserStore()
console.log("userStore.isPassTimeOut::::"+userStore.isPassTimeOut)

const { proxy } = getCurrentInstance();

const user = reactive({
  oldPass: undefined,
  pass: undefined,
  confirmPassword: undefined
});

const equalToPassword = (rule, value, callback) => {
  if (user.pass !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};
//正则校验主要是这部分：/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{8,16}$
//其中特殊字符包括：._~!@#$^&*  （比较常见）
let newValValidate = (rule, value, callback) => {
  if (/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[._~!@#$^&*])[A-Za-z0-9._~!@#$^&*]{8,16}$/g.test(value)) {
    callback()
  } else {
    callback(new Error('请输入包含英文字母大小写、数字和特殊符号的 8-16 位组合'))
  }
}

const rules = ref({
  oldPass: [{ required: true, message: "旧密码不能为空", trigger: "blur" }],
  pass: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
    {
      min: 8,
      max: 16,
      message: '长度应在 8 到 16 个字符',
      trigger: 'blur'
    },
    { validator: newValValidate, trigger: 'blur' }
  ],
  confirmPassword: [{ required: true, message: "确认密码不能为空", trigger: "blur" }, { required: true, validator: equalToPassword, trigger: "blur" }]
});


/** 提交按钮 */
function submit() {
  proxy.$refs.pwdRef.validate(valid => {
    if (valid) {
      request.post("/managerUsers/updatePwd/forManager", {oldPass: Encrypt(user.oldPass),
        pass: Encrypt(user.pass)}).then(response => {
        alert("修改成功");
        open.value = false;
        setPassTimeOut("false");
        userStore.isPassTimeOut = "false";
        router.push({path: "/", query: {} });
        getList();
      });
    }
  });
};

/** 关闭按钮 */
function close() {
  proxy.$tab.closePage();
};
</script>
